<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
      //1. 封装一个函数能输出 今天是周几
     
      let date = new Date();
console.log(date.toDateString());
  
      //2.控制台每隔1s打印时间
      setInterval(nowTime,1000);
    function nowTime(){
        let myDate=new Date();
        let y=myDate.getFullYear();
        let m=myDate.getMonth()+1;      
        let d=myDate.getDate();   
        let H=myDate.getHours();      
        let M=myDate.getMinutes();
        let S=myDate.getSeconds();
        let timeArr=[m,d,H,M,S];
        for(let i=0;i<timeArr.length;i++){
            timeArr[i]=addZero(timeArr[i]);
        }
        let timeStr =`当前时间为:${y}年 ${m}月${d}日${H}时${M}分${S}秒`;
         console.log(timeStr);

        function addZero(n){
            return n>10? n:"0"+n;
        }
          }

      //3.今年双11倒计时
      
      let setT = new Date("2022 11 11 00:00:00");
      let timer;
     
      function timeFn(_t) {
        let t = parseInt((_t.getTime() - new Date().getTime()) / 1000);
        // 天
        let day = parseInt(t / (60 * 60 * 24));
        // 时
        let h = parseInt((t / (60 * 60)) % 24);
        // 分
        let m = parseInt((t / 60) % 60);
        // 秒
        let s = t % 60;

        day = day < 10 ? "0" + day : day;
        h = h < 10 ? "0" + h : h;
        m = m < 10 ? "0" + m : m;
        s = s < 10 ? "0" + s : s;

        if (t > 0) {

          document.querySelector(
            "body"
          ).innerText = `距离${_t.getFullYear()}年双十一活动还有: ${day}天${h}时${m}分${s}秒`;
        } else {

          document.querySelector("body").innerText = "活动结束";
        }
      }

      // 定时器
      timer = setInterval(function () {
        timeFn(setT);
      }, 1000);
    </script>
</body>
</html>